/*     */ package com.zimbra.cs.index.query;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.index.DBQueryOperation;
/*     */ import com.zimbra.cs.index.IntersectionQueryOperation;
/*     */ import com.zimbra.cs.index.NoResultsQueryOperation;
/*     */ import com.zimbra.cs.index.QueryOperation;
/*     */ import com.zimbra.cs.index.UnionQueryOperation;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class InQuery
/*     */   extends Query
/*     */ {
/*     */   private final Folder folder;
/*     */   private final ItemId remoteId;
/*     */   private final String subfolderPath;
/*     */   private final In specialTarget;
/*     */   
/*     */   public static enum In
/*     */   {
/*  44 */     ANY,  LOCAL,  REMOTE,  NONE;
/*     */     
/*     */ 
/*     */     private In() {}
/*     */   }
/*     */   
/*     */ 
/*  51 */   private boolean includeSubfolders = false;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static InQuery create(Mailbox mbox, int fid, boolean under)
/*     */     throws ServiceException
/*     */   {
/*  65 */     if ((under) && (fid == 1)) {
/*  66 */       return new InQuery(null, null, null, In.ANY, under);
/*     */     }
/*  68 */     Folder folder = mbox.getFolderById(null, fid);
/*  69 */     return new InQuery(folder, null, null, null, under);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static InQuery create(Mailbox mailbox, String folder, boolean under)
/*     */     throws ServiceException
/*     */   {
/*  85 */     Pair<Folder, String> result = mailbox.getFolderByPathLongestMatch(null, 1, folder);
/*     */     
/*  87 */     return recursiveResolve(mailbox, (Folder)result.getFirst(), (String)result.getSecond(), under);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static InQuery create(In in, boolean under)
/*     */   {
/*  99 */     return new InQuery(null, null, null, in, under);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static InQuery create(Mailbox mailbox, ItemId iid, String path, boolean under)
/*     */     throws ServiceException
/*     */   {
/* 115 */     if (iid.belongsTo(mailbox)) {
/* 116 */       if ((under) && (iid.getId() == 1)) {
/* 117 */         return new InQuery(null, iid, path, In.ANY, under);
/*     */       }
/*     */       Pair<Folder, String> result;
/*     */       Pair<Folder, String> result;
/* 121 */       if (!Strings.isNullOrEmpty(path)) {
/* 122 */         result = mailbox.getFolderByPathLongestMatch(null, iid.getId(), path);
/*     */       } else {
/* 124 */         Folder folder = mailbox.getFolderById(null, iid.getId());
/* 125 */         result = new Pair(folder, null);
/*     */       }
/* 127 */       return recursiveResolve(mailbox, (Folder)result.getFirst(), (String)result.getSecond(), under);
/*     */     }
/*     */     
/* 130 */     return new InQuery(null, iid, path, null, under);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static InQuery recursiveResolve(Mailbox mailbox, Folder folder, String path, boolean under)
/*     */     throws ServiceException
/*     */   {
/* 141 */     if (!(folder instanceof Mountpoint)) {
/* 142 */       if (path != null) {
/* 143 */         throw MailServiceException.NO_SUCH_FOLDER(folder.getPath() + "/" + path);
/*     */       }
/*     */       
/* 146 */       return new InQuery(folder, null, null, null, under);
/*     */     }
/* 148 */     Mountpoint mpt = (Mountpoint)folder;
/* 149 */     if (mpt.isLocal()) {
/* 150 */       if (Strings.isNullOrEmpty(path)) {
/* 151 */         return new InQuery(folder, null, null, null, under);
/*     */       }
/* 153 */       return recursiveResolve(mailbox, mailbox.getFolderById(null, mpt.getRemoteId()), path, under);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 158 */     return new InQuery(null, mpt.getTarget(), path, null, under);
/*     */   }
/*     */   
/*     */ 
/*     */   private InQuery(Folder folder, ItemId remoteId, String path, In special, boolean under)
/*     */   {
/* 164 */     this.folder = folder;
/* 165 */     this.remoteId = remoteId;
/* 166 */     this.subfolderPath = path;
/* 167 */     this.specialTarget = special;
/* 168 */     this.includeSubfolders = under;
/*     */   }
/*     */   
/*     */   public boolean hasTextOperation()
/*     */   {
/* 173 */     return false;
/*     */   }
/*     */   
/*     */   public QueryOperation compile(Mailbox mbox, boolean bool)
/*     */   {
/* 178 */     if (this.specialTarget != null) {
/* 179 */       if (this.specialTarget == In.NONE)
/* 180 */         return new NoResultsQueryOperation();
/* 181 */       if (this.specialTarget == In.ANY) {
/* 182 */         DBQueryOperation op = new DBQueryOperation();
/* 183 */         op.addAnyFolder(evalBool(bool));
/* 184 */         return op;
/*     */       }
/* 186 */       if (evalBool(bool)) {
/* 187 */         if (this.specialTarget == In.REMOTE) {
/* 188 */           DBQueryOperation dbop = new DBQueryOperation();
/* 189 */           dbop.addIsRemoteClause();
/* 190 */           return dbop;
/*     */         }
/* 192 */         assert (this.specialTarget == In.LOCAL);
/* 193 */         DBQueryOperation dbop = new DBQueryOperation();
/* 194 */         dbop.addIsLocalClause();
/* 195 */         return dbop;
/*     */       }
/*     */       
/* 198 */       if (this.specialTarget == In.REMOTE) {
/* 199 */         DBQueryOperation dbop = new DBQueryOperation();
/* 200 */         dbop.addIsLocalClause();
/* 201 */         return dbop;
/*     */       }
/* 203 */       assert (this.specialTarget == In.LOCAL);
/* 204 */       DBQueryOperation dbop = new DBQueryOperation();
/* 205 */       dbop.addIsRemoteClause();
/* 206 */       return dbop;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 212 */     DBQueryOperation dbOp = new DBQueryOperation();
/* 213 */     if (this.folder != null) {
/* 214 */       if (this.includeSubfolders) {
/* 215 */         List<Folder> subFolders = this.folder.getSubfolderHierarchy();
/*     */         
/* 217 */         if (evalBool(bool))
/*     */         {
/* 219 */           UnionQueryOperation union = new UnionQueryOperation();
/*     */           
/* 221 */           for (Folder sub : subFolders) {
/* 222 */             DBQueryOperation dbop = new DBQueryOperation();
/* 223 */             union.add(dbop);
/* 224 */             if ((sub instanceof Mountpoint)) {
/* 225 */               Mountpoint mpt = (Mountpoint)sub;
/* 226 */               if (!mpt.isLocal()) {
/* 227 */                 dbop.addInRemoteFolder(mpt.getTarget(), "", this.includeSubfolders, evalBool(bool));
/*     */               }
/*     */             }
/*     */             else
/*     */             {
/* 232 */               dbop.addInFolder(sub, evalBool(bool));
/*     */             }
/*     */           }
/* 235 */           return union;
/*     */         }
/*     */         
/* 238 */         IntersectionQueryOperation iop = new IntersectionQueryOperation();
/*     */         
/* 240 */         for (Folder f : subFolders) {
/* 241 */           DBQueryOperation dbop = new DBQueryOperation();
/* 242 */           iop.addQueryOp(dbop);
/* 243 */           if ((f instanceof Mountpoint)) {
/* 244 */             Mountpoint mpt = (Mountpoint)f;
/* 245 */             if (!mpt.isLocal()) {
/* 246 */               dbop.addInRemoteFolder(mpt.getTarget(), "", this.includeSubfolders, evalBool(bool));
/*     */             }
/*     */             
/*     */           }
/*     */           else
/*     */           {
/* 252 */             dbop.addInFolder(f, evalBool(bool));
/*     */           }
/*     */         }
/* 255 */         return iop;
/*     */       }
/*     */       
/* 258 */       dbOp.addInFolder(this.folder, evalBool(bool));
/*     */     }
/* 260 */     else if (this.remoteId != null) {
/* 261 */       dbOp.addInRemoteFolder(this.remoteId, this.subfolderPath, this.includeSubfolders, evalBool(bool));
/*     */     }
/* 263 */     else if (!$assertionsDisabled) { throw new AssertionError();
/*     */     }
/*     */     
/* 266 */     return dbOp;
/*     */   }
/*     */   
/*     */   public Folder getFolder() {
/* 270 */     return this.folder;
/*     */   }
/*     */   
/*     */   public void dump(StringBuilder out)
/*     */   {
/* 275 */     out.append(this.includeSubfolders ? "UNDER:" : "IN:");
/* 276 */     if (this.specialTarget != null) {
/* 277 */       switch (this.specialTarget) {
/*     */       case ANY: 
/* 279 */         out.append("ANY_FOLDER");
/* 280 */         break;
/*     */       case LOCAL: 
/* 282 */         out.append("LOCAL");
/* 283 */         break;
/*     */       case REMOTE: 
/* 285 */         out.append("REMOTE");
/* 286 */         break;
/*     */       case NONE: 
/* 288 */         out.append("NONE");
/*     */       }
/*     */     }
/*     */     else {
/* 292 */       out.append(this.folder != null ? this.folder.getName() : this.remoteId != null ? this.remoteId.toString() : "ANY_FOLDER");
/* 293 */       if (this.subfolderPath != null) {
/* 294 */         out.append('/');
/* 295 */         out.append(this.subfolderPath);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void sanitizedDump(StringBuilder out)
/*     */   {
/* 302 */     out.append(this.includeSubfolders ? "UNDER:" : "IN:");
/* 303 */     if (this.specialTarget != null) {
/* 304 */       switch (this.specialTarget) {
/*     */       case ANY: 
/* 306 */         out.append("ANY_FOLDER");
/* 307 */         break;
/*     */       case LOCAL: 
/* 309 */         out.append("LOCAL");
/* 310 */         break;
/*     */       case REMOTE: 
/* 312 */         out.append("REMOTE");
/* 313 */         break;
/*     */       case NONE: 
/* 315 */         out.append("NONE");
/*     */       }
/*     */     }
/*     */     else {
/* 319 */       out.append(this.folder != null ? "$FOLDER" : this.remoteId != null ? "$REMOTEFOLDER" : "ANY_FOLDER");
/* 320 */       if (this.subfolderPath != null) {
/* 321 */         out.append('/');
/* 322 */         out.append("$SUBFOLDER");
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/query/InQuery.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */